import cTitle from "components/title";
import DList from "@/components/dlist";
export default {
  data() {
    return {
      memberInfo: {},
      newGoodList: [],
      info: false,
      loading: false,
      finished: false,
      page: 1,
      total: 0,
      is_show: false
    };
  },

  watch: {
    $route: function(to, from) {
      document.body.scrollTop = 0;
      document.documentElement.scrollTop = 0;
    }
  },

  mounted() {
    window.scrollTo(0, 0);
  },

  methods: {
    //获取会员信息
    getMemberInfo() {
      $http
        .get("plugin.video-demand.api.video-demand-member.get-member-data", {})
        .then(response => {
          if (response.result === 1) {
            this.memberInfo = response.data;
            this.getRecommendData();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    //跳转购买vip
    gotoVip() {
      this.$router.push(this.fun.getUrl("CourseMemberUpdate"));
    },
    //获取推荐商品数据
    async getRecommendData() {
      if (this.loading === true || this.finished === true) {
        return;
      }
      this.loading = true;
      let { data, result, msg } = await $http.get('plugin.video-demand.api.video-demand-member.get-recommend-goods', { page: this.page });
      if (result === 0) {
        this.$toast(msg);
        return;
      }
      if (data.data.length === 0 || data.data.length < data.per_page || data.current_page === data.last_page) {
        this.finished = true;
      }
      this.total = data.total;
      this.newGoodList.push(...data.data);
      this.page++;
      this.loading = false;
      this.info = true;
    },
    onLoad() {
      this.getRecommendData();
    },
    //更多商品
    moreGoods(type) {
      this.$router.push(
        this.fun.getUrl("CourseSearch", { goods_type: type, fromHome: 1 })
      );
    },
    //商品详情
    goToDetail(id) {
      $http
        .get(
          "plugin.video-demand.api.video-course-goods.get-course-goods-detail",
          { goods_id: id }
        )
        .then(response => {
          if (response.result == 1) {

            this.$router.push(this.fun.getUrl("goods", { id: id }));
            // if (response.data.is_course == 1) {
            //   this.$router.push(
            //     this.fun.getUrl("CourseDetail", { id: id })
            //   );
            // } else {
            //   this.$router.push(this.fun.getUrl("goods", { id: id }));
            // }
          }else {
            this.$toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
  },
  activated() {
    this.getMemberInfo();
    document.getElementById("course").scrollTop = "40px";
  },

  components: { cTitle, DList }
};
